package com.haha.onebot.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.haha.onebot.constant.ActionType;
import com.haha.onebot.entity.Bot;
import com.haha.onebot.entity.BotMessage;
import com.haha.onebot.entity.OneBotMessage;
import com.haha.onebot.entity.ReplyMessage;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import java.util.HashMap;
import java.util.Map;


@Log4j2
@Component
public class NoticeEventListener {

    public static final String GROUP_ID = "group_id";
    public static final String MESSAGE = "message";
    @Resource
    private Bot bot;

    @Resource
    private ObjectMapper objectMapper;

    @Async
    @EventListener(classes = BotMessage.class, condition = "#botMessage.id == 'notice'")
    public void handlerResult(BotMessage botMessage) {
        OneBotMessage oneBotMessage = botMessage.getOneBotMessage();
        Long groupId = oneBotMessage.getGroupId();
        boolean groupDecrease = oneBotMessage.getNoticeType().equals("group_decrease");
        ReplyMessage replyMessage = new ReplyMessage();
        replyMessage.setAction(ActionType.SEND_GROUP_MSG);
        Long userId = oneBotMessage.getUserId();
        // 机器人不响应 q群官家 机器人qq号
        if(userId.equals(3889047445L) || userId.equals(bot.getBotId())) {
            return;
        }

        // 群成员减少
        if (groupDecrease) {
            Map<String, Object> params = new HashMap<>();
            params.put(GROUP_ID, oneBotMessage.getGroupId());
            params.put(MESSAGE, "小伙伴"
                    + "(" + userId + ")离开了我们，让我们缅怀他吧");
            replyMessage.setParams(params);
            try {
                String reply = objectMapper.writeValueAsString(replyMessage);
                bot.getSession().sendMessage(new TextMessage(reply));
            } catch (Exception e) {
                log.error("groupDecrease 消息发送失败：{}", e);
            }

        }
        boolean groupIncrease = oneBotMessage.getNoticeType().equals("group_increase");
        // 群成员增加
        if (groupIncrease) {

            Map<String, Object> params = new HashMap<>();
            params.put(GROUP_ID, oneBotMessage.getGroupId());
            params.put(MESSAGE, "呦，大爷，来玩了\uD83D\uDE0F" + "\n [CQ:at,qq=" + userId + "] ");
            replyMessage.setParams(params);
            try {
                String reply = objectMapper.writeValueAsString(replyMessage);
                bot.getSession().sendMessage(new TextMessage(reply));
            } catch (Exception e) {
                log.error("groupIncrease 消息发送失败：{}", e);
            }

        }

        // 戳一戳事件
        boolean notify = oneBotMessage.getNoticeType().equals("notify");
        if (notify) {
            if (oneBotMessage.getSubType().equals("poke")) {
                if (oneBotMessage.getTargetId().equals(bot.getBotId())) {
                    Map<String, Object> params = new HashMap<>();
                    params.put(GROUP_ID, oneBotMessage.getGroupId());
                    params.put(MESSAGE, "哎呦，不要碰人家嘛，干嘛啦~~(✿◡‿◡)\uD83D\uDE0F" + "\n [CQ:at,qq=" + userId + "] ");
                    replyMessage.setParams(params);
                    try {
                        String reply = objectMapper.writeValueAsString(replyMessage);
                        bot.getSession().sendMessage(new TextMessage(reply));
                    } catch (Exception e) {
                        log.error("poke 消息发送失败：{}", e);
                    }
                }
            }
        }


        log.info("notice:{}", botMessage);
    }

}
